/*
 * TOF10120.c
 *
 *  Created on: Jul 15, 2021
 *      Author: 本地账户
 */

#include "TOF10120.h"


/*******************************************************************************
* Function Name  : SensorWritenByte
* Description    : Sensor read api.
* Input          : *txbuff-the buffer which to write.
*                : regaddr-the address write to
*                : size-write data size
* Output         : None
* Return         : None
*******************************************************************************/
unsigned char SensorWritenByte(unsigned char *txbuff, unsigned char regaddr, unsigned char size)
{
	unsigned char i = 0;

	IIC2_Start();

  IIC2_Send_Byte( I2C_DEVID|0x00 );
  if( 1 == IIC2_Wait_Ack() )
  {
		IIC2_Stop();
    return 1;
  }
  IIC2_Send_Byte( regaddr&0xff );
  if( 1 == IIC2_Wait_Ack() )
  {
		IIC2_Stop();
    return 1;
  }
	for ( i = 0; i < size; i++)
	{
		IIC2_Send_Byte( txbuff[size-i-1] );
		if( 1 == IIC2_Wait_Ack() )
		{
			IIC2_Stop();
			return 1;
		}
	}
  IIC2_Stop();

  return 0;
}

/*******************************************************************************
* Function Name  : SensorReadnByte
* Description    : Sensor read api.
* Input          : *rxbuff-the buffer which stores read content.
*                : regaddr-the address read from
*                : size-read data size
* Output         : None
* Return         : None
*******************************************************************************/
unsigned char SensorReadnByte(unsigned char *rxbuff, unsigned char regaddr, unsigned char size)
{
  unsigned char i = 0;

  IIC2_Start();

  IIC2_Send_Byte( I2C_DEVID|0x00 );
  if( 1 == IIC2_Wait_Ack() )
  {
	  IIC2_Stop();
    return 1;
  }
  IIC2_Send_Byte( regaddr&0xff );
  if( 1 == IIC2_Wait_Ack() )
  {
	  IIC2_Stop();
    return 1;
  }
  IIC2_Stop();
  IIC2_Start();
  IIC2_Send_Byte( I2C_DEVID|0x01 );
  if( 1 == IIC2_Wait_Ack() )
  {
	  IIC2_Stop();
    return 1;
  }
  delay_us(30);  //30uS
	for ( i = 0; i < size; i++)
	{
		rxbuff[size-i-1] = IIC2_Read_Byte();
		if((i+1)==size)
			IIC2_NAck();
		else
			IIC2_Ack();
		delay_us(30);//30uS
	}
  IIC2_Stop();

  return 0;
}


